///*
// * @(#)DiagnosisPayTradeResultProcessor.java 创建于 2016-07-19 20:31:27
// *
// * 版权：版权所有 Bsoft 保留所有权力。
// */
//package hcn.ods.pay.diagnosis;
//
//import ctd.controller.exception.ControllerException;
//import hcn.base.pay.dao.PayTradeDAO;
//import hcn.base.pay.event.PayEvent;
//import hcn.base.pay.event.PayEventManager;
//import hcn.base.pay.service.PayTradeResultProcessor;
//import hcn.common.CodeDefinition;
//import hcn.ods.PayTrade;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.Map;
//
///**
// * @author <a href="mailto:chinnsii@163.com">zhengshi</a>
// */
//public class DiagnosisPayTradeResultProcessor implements PayTradeResultProcessor {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(DiagnosisPayTradeResultProcessor.class);
//
//    private PayTradeDAO payTradeDAO;
//    private PayEventManager payEventManager;
//    private String adapterService;
//    private String notifyMethodName;
//
//    /**
//     * @param payTrade
//     * @param adapterResponse
//     * @throws ControllerException
//     */
//    @Override
//    @SuppressWarnings("unchecked")
//    public void processAdapterCalled(PayTrade payTrade, Object adapterResponse) throws ControllerException {
//        String resultId = null;
//        if (adapterResponse != null) {
//            Map<String, Object> response = (Map<String, Object>) adapterResponse;
//            LOGGER.debug("HIS returned data: {}", response);
//            int resultCode = (int) response.get("resultCode");
//            if (resultCode == 3) {
//                String tradeNo = (String) response.get("hcnTradeNo");
//                LOGGER.info("HIS returned result code: {}, and tradeNo: {}", resultCode, tradeNo);
//                if (!payTrade.getTradeNo().equals(tradeNo)) {
//                    throw new ControllerException(CodeDefinition.DUPLICATED_PAYMENT, "Duplicated payment, previous trade no is：" + tradeNo);
//                }
//            }
//
//            resultId = (String) response.get("resultId");
//        }
//        if (StringUtils.isNotEmpty(resultId)) {
//            payTrade.setExternalTradeNo(resultId);
//        } else {
//            throw new ControllerException(CodeDefinition.NOTIFY_PAY_RESULT_FAILED, "His response: " + adapterResponse);
//        }
//    }
//
//    /**
//     * @param payTrade
//     * @throws ControllerException
//     */
//    @Override
//    public void processTradeSuccess(PayTrade payTrade) throws ControllerException {
//        PayEvent payEvent = new PayEvent(payTrade);
//        payEvent.setStatus(PayEvent.TradeStatus.SUCCESS);
//        payEventManager.fireEvent(payEvent, true);
//    }
//
//    /**
//     * @param payTrade
//     * @throws ControllerException
//     */
//    @Override
//    public void processTradeFailed(PayTrade payTrade) throws ControllerException {
//        PayEvent payEvent = new PayEvent(payTrade);
//        payEvent.setStatus(PayEvent.TradeStatus.FAILED);
//        payEventManager.fireEvent(payEvent, true);
//    }
//
//    public PayTradeDAO getPayTradeDAO() {
//        return payTradeDAO;
//    }
//
//    public void setPayTradeDAO(PayTradeDAO payTradeDAO) {
//        this.payTradeDAO = payTradeDAO;
//    }
//
//    public PayEventManager getPayEventManager() {
//        return payEventManager;
//    }
//
//    public void setPayEventManager(PayEventManager payEventManager) {
//        this.payEventManager = payEventManager;
//    }
//
//    @Override
//    public String getAdapterService() {
//        return adapterService;
//    }
//
//    public void setAdapterService(String adapterService) {
//        this.adapterService = adapterService;
//    }
//
//    @Override
//    public String getNotifyMethodName() {
//        return notifyMethodName;
//    }
//
//    public void setNotifyMethodName(String notifyMethodName) {
//        this.notifyMethodName = notifyMethodName;
//    }
//}
